package sy8_4;

import com.sun.source.tree.IfTree;

import javax.naming.Name;
import java.util.*;

public class demo {
    public static void main(String[] args) {
        TreeMap<String,String> treeMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return -o1.compareTo(o2);
            }
        });
        treeMap.put("7","Lucy");
        treeMap.put("3","Alic");
        treeMap.put("4","John");
        treeMap.put("5","tom");
        System.out.println("逆存放："+treeMap);
        System.out.println("=====================================");
        HashMap<String,String> map=new HashMap();
        put(treeMap,map);
        System.out.println("map集合："+map);
        System.out.println("=====================================");
        HashSet set= new HashSet();
        keyset(map,set);
        System.out.println("set集合："+set);
        System.out.println("=====================================");
        System.out.println("3对应的键为："+get(treeMap,"3"));
        System.out.println("4对应的键为："+get(treeMap,"4"));
        System.out.println("5对应的键为："+get(treeMap,"5"));
        System.out.println("7对应的键为："+get(treeMap,"7"));

    }
    private static void put(TreeMap<String,String> treeMap,HashMap<String,String> map){
        Set<Map.Entry<String, String>> entries = treeMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            String name = entry.getValue();
            String id = entry.getKey();
            map.put(name,id);

        }
    }
    private static HashSet keyset(HashMap<String,String> map,HashSet set){

        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            String id =entry.getValue();
            set.add(id);
        }
        return set;
    }
    private static String get(TreeMap<String,String> treeMap,String id){
        String Name = null;
        Set<Map.Entry<String, String>> entries = treeMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            if (id == entry.getKey()) Name=entry.getValue();
        }
        return Name;
    }
}
